import type { IUserVo } from "@/logic/globInterface/dataInterface";
import type { promises } from "fs";
import { HTTP_API_URL } from "../consts/Consts";
import LocalData from "../localData/LocalData";
import LogUtil, { LOG_LEVEL } from "../utils/LogUtil";
import { platformHelper } from "../utils/PlatformHelper";
import { SingletonTemplet } from "../utils/SingletonTemplet";
import { HttpMethod, httpUtil } from "./httpUtil";
export interface IRequireData{
	api:string,
	method:HttpMethod,
	params?:string | AnyObject | ArrayBuffer,
	showLoding:boolean,
	errorRetry:boolean
}
/**
 * 数据接口管理对象
 */
export default class NetData extends SingletonTemplet{
	/**缓存请求队列*/
	private requireDataMap:Map<number,IRequireData> = new Map<number,IRequireData>();
	/**请求flag*/
	private reqId:number = 0;
	/**发生错误时的重尝试次数*/
	private errorTryNum:number = 0;
	/**发生错误时的重尝试次数*/
	private maxErrorTryNum:number = 3;
	/**
	 * 示例
	 */
	public async queryBaseUser(upkId:string){
		let reqData:IRequireData = {
			api:HTTP_API_URL.USER_LOGIN,
			method:HttpMethod.POST,
			params:upkId,
			showLoding:false,
			errorRetry:true,
		}
		let data =  await this.requireData(reqData);
		return data;
	}
	/**
	 * 
	 * 请求网络数据
	 * @param api:接口地址
	 * @param method:提交类型
	 * @param params:提交参数
	 * @param showLoding:是否显示loading
	 * @param errorRetry:发生错误时是否进行重试
	 */
	private async requireData(reqireData:IRequireData):Promise<any>{
	   this.errorTryNum = 0 ;
	   if(reqireData.showLoding){
		   let isSuc = platformHelper.showLoadIng(reqireData.showLoding);
		   if(!isSuc){
			   LogUtil.log(LOG_LEVEL.ERROR,"requireData","显示loading提示错误");
		   }
	   }
	   let reqId =  this.addRequireData(reqireData);
	   let data = null;
	   try{
			 data = httpUtil.request(reqireData.api,reqireData.method,reqireData.params)
	   }catch(e){
		   if(reqireData.errorRetry){
				data = this.reTryReqireData(reqireData);
				if(data == null){
					uni.showModal({
						showCancel:true,
						cancelText:"取消",
						confirmText:"是否进行重试?",
						fail: (result) => {
							 LogUtil.log(LOG_LEVEL.ERROR,"requireData","提示框弹出失败"+result);
						},
						success: async() => {
							this.removeRequireData(reqId);
						}
					})
				}
		   }else{
			   uni.showToast({
			   	title:e.message,
			   	icon: "none",
			   	duration: 3000
			   })
			   LogUtil.log(LOG_LEVEL.ERROR,"requireData","请求数据报错");
		   }
			
			this.removeRequireData(reqId);
	   }
	   return data;
	}
	/**
	 * 重试请求
	 */
	private async reTryReqireData(reqireData:IRequireData) {
		let data :any= null;
		this.errorTryNum++;
		try{
			data = await httpUtil.request(reqireData.api,reqireData.method,reqireData.params)
		}catch(e){
			if(this.errorTryNum > this.maxErrorTryNum){
				return null;
			}
			data = this.reTryReqireData(reqireData);				
		}
		return data;
	}
	/**
	 * 请求数据入队
	 */
	private addRequireData(reqireData:IRequireData){
		let reqId =  this.reqId++;
		this.requireDataMap.set(reqId,reqireData);
		return reqId;
	}
	/**
	 * 请求数据成功 移除
	 */
	private removeRequireData(reqId:number){
		this.requireDataMap.delete(reqId);
	}
}